package com.songlanyun.modules.article.controller.api;

import com.songlanyun.common.annotation.RepeatSubmit;
import com.songlanyun.common.enums.CommentConstant;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.article.entity.Article;
import com.songlanyun.modules.article.model.vo.ArticleVO;
import com.songlanyun.modules.article.service.ArticleService;
import com.songlanyun.modules.comment.entity.UserComment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 文章表
 *
 * @author wuchao
 * @email sunlightcs@gmail.com
 * @date 2021-02-27 15:36:01
 */
@RestController
@RequestMapping("api/bbs/article")
public class ArticleApiController extends AbstractApiController {
    @Autowired
    private ArticleService articleService;

    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("article:article:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = articleService.appList(params);

        return R.ok().put("page", page);
    }

    /**
     * 推荐列表
     */
    @GetMapping("/recommend-list")
    //@RequiresPermissions("article:article:info")
    public R recommendList(@RequestParam Map<String, Object> params) {
        PageUtils page = articleService.recommendList(params);
        return R.ok().put("page", page);
    }

    /**
     * 热门列表
     */
    @GetMapping("/hot-list")
    //@RequiresPermissions("article:article:info")
    public R hotList(@RequestParam Map<String, Object> params) {
        PageUtils page = articleService.hotList(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("article:article:info")
    public R info(@PathVariable("id") Long id) {

        ArticleVO vo = articleService.appInfo(getUserId(false),id);
        return R.ok().put("data", vo);
    }

    /**
     * 评论文章
     */
    @RepeatSubmit("1")
    @PostMapping("/comment")
    public R save(@RequestBody UserComment userComment) {
        ValidatorUtils.validateEntity(userComment);
        userComment.setCommentUserId(getUserId(true));
        userComment.setModelType(CommentConstant.CommentModelType.ARTICLE);
        articleService.comment(userComment);

        return R.ok();
    }

    /**
     * 取消点赞文章
     */
    @RepeatSubmit("1")
    @PostMapping("/cancel-like/{id}")
    public R cancelLike(@PathVariable("id") Long id) {
        Map<String, Object> map = articleService.cancelLike(id, getUserId(true));
        return R.ok().put("map", map);
    }


}
